var attach = function(){
    var obj = this;
    this.init = function(){
        $( ".add_attach, .thumb_attach, .del_attach" ).click( function () {
            var o = $( this );
            if ( o.hasClass( 'disabled' ) ) {
                return false;
            }
            var c = o.attr( 'class' ).split( ' ' )[0].split( '_' )[0];
            var fun = obj[c];
            fun( o, o.parent().parent() );
            return false;
        });

        obj.Preview( $( "tr.attach .name" ), 'img', 'file' );
    }
    this.Preview = function( a, b, c ){
        var xOffset = 10;
        var yOffset = 80;
        var id = "imgPreview";
        a.hover( function ( e ) {
            $( "#imgPreview" ).remove();
            var file = host + '/' + $( this ).parent().find( '.' + c ).html();
            var img = $( this ).parent().find( '.' + b ).html();
            if ( img == 1 ) {
                $( "<img id='" + id + "' src='" + file + "' />" ).appendTo( "body" );
                $( "#" + id ).css( "top", ( e.pageY - xOffset) + "px" ).css( "left", ( e.pageX + yOffset) + "px" );
                $( "#" + id ).animate({
                    maxHeight: "320",
                    maxWidth: "500"
                }, 600, 'swing');

            }
        }, function () {
            $( "#" + id ).remove();
        });
    }
    this.thumb = function( a, b ){
        var file = b.find( '.file' ).html();
        $( 'input[name="article[thumb]"]' ).val( file ).focus();
    }
    this.del = function( a, b ){
        var url = a.attr( 'href' );
        var img = b.find( ".img" ).html();
        var file = img == 1 ? b.find( ".file" ).html() : b.find( ".id" ).html();
        if ( editor1.designMode == false ) {
            $.jBox.tip( '请先将编辑器切换到可视化模式!' );
            return false;
        }
        $.get( url + "&ajax=1", function ( result ) {
            $.jBox.tip( '正在删除附件...', 'loading', {opacity:0.2} );
            if ( result == 'success' ) {

                // 删除文章内容里面的附件
                var html = editor1.html();
                var src = obj.formatEditHtml( img, file );
                var newHTML = html.replaceAll( src, '' );
                editor1.html( newHTML );

                // 删除缩略图
                var _thumb = $( 'input[name="article[thumb]"]' );
                if( file == _thumb.val() ){
                    _thumb.val('' ).focus();
                }

                window.setTimeout( function () {
                    $.jBox.tip( '删除附件完成', 'success' );
                    b.remove();
                }, 2000 );
                window.setTimeout( function () {
                    if( $("tr.attach" ).size() <= 0 ){
                        window.location.reload();
                    }
                }, 3000 );
                return;
            }
            $.jBox.closeTip();
            $.jBox( "<div style='padding: 10px;'>" + result + "</div>", { title:'提示', width:'auto' } )
        });
    }
    this.add = function( a, b ){
        var img = b.find( ".img" ).html();
        var file = img == 1 ? b.find( ".file" ).html() : id;
        var html = obj.formatEditHtml( img, file );
        insert( html );
    }
    this.formatEditHtml = function( type, file ){
        if( type == 1 ){
            file = host + '/' +  file;
            return '<img src="' + file + '" />';
        }
        return "[attach=" + file + "]";
    }
}
$( function () {
    new attach().init();
});